import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Scanner;

import javax.swing.JTextArea;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;



public class Connection 
{
	private static boolean USBtest = false;
	static long start = 0, latency = 0;
	static Boolean readFlag = true;
	static Object lock = new Object();
	private static NXTComm connection;
	private static DataOutputStream oHandle;
	private static DataInputStream iHandle;
	static Boolean bluetoothConnect;

	public static void connect() throws NXTCommException {

		start = System.currentTimeMillis();
		NXTInfo info;

		connection = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
		info = new NXTInfo(NXTCommFactory.BLUETOOTH, "BURNINATOR", "00:16:53:12:3D:19"); // your robot's name must be NXT and the code is 1234
	//	info = new NXTInfo(NXTCommFactory.BLUETOOTH, "Team9", "00:16:53:13:E5:96"); // your robot's name must be NXT and the code is 1234
	//	info = new NXTInfo(NXTCommFactory.BLUETOOTH, "nxj-003","00:16:53:12:46:55");
	//	info = new NXTInfo(NXTCommFactory.BLUETOOTH, "7a","00:16:53:13:F6:A4");
		// open connections and data streams
		connection.open(info);

		latency = System.currentTimeMillis() - start;
		System.out.printf("Connection is established [%dms]\n", latency);
		bluetoothConnect = true;

	}

	public static void read(final JTextArea display) throws NXTCommException
	{						
		//System.out.println("test");

		InputStream is = connection.getInputStream();

		iHandle = new DataInputStream(is);

		Thread PCreceiver = new Thread()
		{
			public void run() {

				while (readFlag) {
					try {
						start = System.currentTimeMillis();
						byte[] buffer = new byte[256];
						int count = iHandle.read(buffer); // might want to check back later
						if (count>0){
							String ret = (new String(buffer)).trim();
							long l = System.currentTimeMillis() - start;
							System.out.printf("NXJ: %s [%dms]\n", ret, l);
							display.append(ret+"\n");
						}
						Thread.sleep(10);
					} catch (IOException e) {
						System.out.println("Fail to read from iHandle bc "
								+ e.toString());
						return;
					} catch (InterruptedException e){

					}
				}
			}
		};
		PCreceiver.start();
		//bluetoothConnect = true;
		//System.out.println("\nPC: CONNECTED...");
	}

	public static boolean isConnected() 
	{
		return bluetoothConnect;
	}

	public static void send(int input)throws NXTCommException
	{

		OutputStream os = connection.getOutputStream();
		oHandle = new DataOutputStream(os);


		try {
 
				start = System.currentTimeMillis();
				oHandle.writeInt(input);
				oHandle.flush();
				latency = System.currentTimeMillis() - start;

				System.out.println("\nPC: " + input + " [" + latency + "ms]");
 

		} catch (IOException e) {
			System.out.println("Fail to write to oHandle bc "
					+ e.toString());
			return;
		}



	}

	public static void closeConnection(){ //changed to static
		try {
			connection.close();
			readFlag=false; // stop reading threads
			System.out.println("DISCONNECTED");
			
			// stop all threads as well 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
